package com.ly.listener;

import com.ly.bean.EasybuyOrderDetail;
import com.ly.bean.EasybuyProduct;
import com.ly.mapper.EasybuyProductMapper;
import com.ly.service.impl.ProductCategoryServiceImpl;
import com.ly.util.QueueNameUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author 李岩
 * @date 2022/1/11-14:58
 * @describe
 */
@Component
@RabbitListener(queues = QueueNameUtil.FANOUT_QUEUE_NAME_PROD)
public class OrderLintener {
    private final Logger logger= LoggerFactory.getLogger(OrderLintener.class);
    private OrderLintener(){
        System.out.println("进入到OrderLintener 的product");
    }

    @Autowired
    private EasybuyProductMapper productMapper;

    @RabbitHandler
    public void process(Map obj){

        try {
            logger.info("商品模块下收到的订单信息为  "+obj);
            List<EasybuyOrderDetail> orderDetailList =(List<EasybuyOrderDetail>) obj.get("orderDetailList");

            if (orderDetailList!=null && orderDetailList.size()>0){
                for (EasybuyOrderDetail orderDetail : orderDetailList) {
                    EasybuyProduct product =(EasybuyProduct) productMapper.selectProductByPrimaryKey(orderDetail.getProductid());

                    product.setStock(product.getStock()-orderDetail.getQuantity());
                    productMapper.updateByPrimaryKeySelective(product);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
